<template>
	<view class="shzy-select">
		<u-input type="select" :select-open="selectShow" v-model="innerValue" :placeholder="placeholder"
			@click="openSelect"></u-input>
		<shzy-lazy-select v-if="lazy" :list="newOptions" v-model="selectShow" @confirm="selectConfirm" :value-name="props.value"
			:label-name="props.label" :default-value="defaultValue" :isConcat="isConcat" :concatSymbol="concatSymbol" ></shzy-lazy-select>	
			
		<u-select v-if="!multiple && !lazy" :list="newOptions" v-model="selectShow" @confirm="selectConfirm" :value-name="props.value"
			:label-name="props.label" :default-value="defaultValue"  />

		<mult-select v-if="multiple" :list="newOptions" v-model="selectShow" @confirm="selectConfirm" :value-name="props.value"
			:label-name="props.label" :default-value="defaultValue"  />
	</view>
</template>

<script>
	import multSelect from './mult-select'
	export default {
		name: 'shzy-select',
		components: {
			multSelect
		},
		model: {
			prop: 'value',
			event: 'input'
		},
		props: {
			value: {
				default: ''
			},
			placeholder: {
				type: String,
				default: '请选择'
			},
			options: {
				type: Array,
				default: () => []
			},
			props: {
				type: Object,
				default: () => ({
					label: 'fullName',
					value: 'id'
				})
			},
			disabled: {
				type: Boolean,
				default: false
			},
			multiple: {
				type: Boolean,
				default: false
			},
			//  是否懒加载数据
			lazy: {
				type: Boolean,
				default: false
			},
			// 是否拼接 label value
			isConcat: {
				type: Boolean,
				default:false
			},
			// 拼接label、value符号
			concatSymbol: {
				type: String,
				default: ' | '
			},
		},
		computed: {
			newOptions() {
				return this.options.map((o, i) => ({
					...o,
					extra: i,
				}))
			}
		},
		watch: {
			// options(val){
			// 	if(val) this.setDefault()
			// },
			newOptions() {
				this.setDefault()
			},
			
			value(val) {
				this.setDefault()
			},
			defaultValue: {
				handler(val) {
				},
				immediate: true,
			}
		},
		data() {
			return {
				selectShow: false,
				innerValue: '',
				defaultValue: []
			}
		},
		created() {
			this.setDefault()
		},
		methods: {
			scroll(){
			 console.log(222)	
			},
			scrolltolower(){
				console.log(123)
			},
			openSelect() {
				if (this.disabled) return
				this.selectShow = true
			},
			selectConfirm(e, label = '') {
				if (this.multiple) {
					this.$emit('input', e)
					this.$emit('change', e)
					this.$nextTick(() => {
						this.$emit('changeFun', e)
					})
					this.defaultValue = e
					this.innerValue = label
				} else {
					this.$emit('input', e[0].value || '')
					this.$emit('change', e[0].value || '')
					this.$nextTick(() => {
						this.$emit('changeFun', e[0].value || '')
					})
					this.innerValue = this.isConcat ? e[0].label+' | '+ e[0].value :  e[0].label || ''
					if (e[0].extra === undefined || e[0].extra === null || e[0].extra === '') return
					this.defaultValue = [e[0].extra]
				}
				
			},
			setDefault() {
				if (this.multiple) {
					if (!this.value.length) return this.innerValue = ''
					this.defaultValue = this.value
					let label = ''
					let value = ''
					outer: for (let i = 0; i < this.defaultValue.length; i++) {
						inner: for (let j = 0; j < this.options.length; j++) {
							if (this.defaultValue[i] === this.options[j][this.props.value]) {
								if (!label) {
									label = this.options[j][this.props.label]
								} else {
									label += ',' + this.options[j][this.props.label]
								}
								break inner
							}
						}
					}
					this.innerValue = label
				}
				// else if(this.lazy){
				// 	if (!this.value) return this.innerValue = ''
				// 	for (let i = 0; i < this.options.length; i++) {
				// 		console.log(this.options[i][this.props.value],this.value)
				// 		if (this.options[i][this.props.value] == this.value) {
						
				// 			this.defaultValue = [i]
				// 			this.innerValue = this.options[i][this.props.label]
				// 			break
				// 		}
				// 	}
				// } 
				else {
					if (!this.value) return this.innerValue = ''
					for (let i = 0; i < this.options.length; i++) {
						if (this.options[i][this.props.value] === this.value) {
							this.defaultValue = [i]
							this.innerValue = this.isConcat ? this.options[i][this.props.label]+' | '+this.options[i][this.props.value] : this.options[i][this.props.label]
							break
						}
					}
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	.shzy-select {
		width: 100%;
	}
</style>
